// pages/news/news.js
var url = "https://www.hkzhaoscut.top/normalUser/getNews";
var page = 0;
var page_size = 5; 
var loadMore = function(that) {
  wx.showLoading({
    title: "加载中",
  })  
  wx.request({
    url: url,
    data: {
      pageNum: page,
      pageSize: page_size
    },
    success: function(res) {
      // console.log(res.data)
      wx.hideLoading();
      var code = res.data.code
      var list = res.data.list
      var tmplist = []
      if (code == 200) {
        // 同样fake_data填充
        list = data_news.page1.news
        // 以上
        if (list != null) {
          page++;
          list.forEach(
            function(item, index) {
              tmplist[index] = {}
              var date = new Date(item.createTime)
              tmplist[index].createTime = util.formatTimeWithoutYear(date)
              tmplist[index].title = item.title
              tmplist[index].text = util.subText(item.text)
              tmplist[index].newsId = item.newsId
            }
          )
          var originNews = that.data.news
          var curNews = originNews.concat(tmplist)
          that.setData({
            news: curNews
          })
        }


      }

    }  
  });
}
var loadInit = function(that) {
  wx.request({
    url: url,
    data: {
      pageNum: 0,
      pageSize: page_size
    },
    success: function(res) {
      var code = res.data.code
      var list = res.data.list
      var tmplist = []
      if (code == 200) {
        page++;
        // 以下，此段是fake——data测试
        list = data_news.page1.news
        //以上
        if (list != null) {
          list.forEach(
            function(item, index) {
              tmplist[index] = {}
              var date = new Date(item.createTime)
              tmplist[index].createTime = util.formatTimeWithoutYear(date)
              tmplist[index].title = item.title
              tmplist[index].text = util.subText(item.text)
              tmplist[index].newsId = item.newsId
            }
          )
          var curNews = tmplist
          that.setData({
            news: curNews
          })
        }


      }

    }
  });
}
var data_news = require("../../data/data_news.js")
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    news: []
  },
  // 点击每个卡片进入新闻详情,组件内返回的是新闻的id
  toDetail(e) {
    var news_id = e.detail.news_id
    wx.navigateTo({
      url: '../newsDetail/newsDetail?news_id=' + news_id,
    })

  },
  scrolltolower(e) {
    // 拉到底再获取后续资料
    var that = this
    loadMore(that)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    loadInit(that)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})